package com.czkj.hwz.decorate.Activity;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.czkj.hwz.decorate.R;
import com.czkj.hwz.decorate.adapter.SupervisorAdapter;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Rows;
import com.google.gson.Gson;
import com.king.base.BaseActivity;
import com.king.base.util.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.finalteam.okhttpfinal.HttpCycleContext;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.JsonHttpRequestCallback;
import cn.finalteam.okhttpfinal.RequestParams;
import okhttp3.Headers;



/**
 * 监理搜索页面
 * @author lxm
 * created at 2017/10/18 20:25
 */
public class SupervisorSearchActivity extends BaseActivity implements HttpCycleContext {
    protected final String HTTP_TASK_KEY = "HttpTaskKey_" + hashCode();
    private static final String TAG = "SupervisorSearchActivit";
    private EditText search_text;
    private List<Rows> rowsList;
    private SupervisorAdapter supervisorAdapter;
    private ListView lv;

    private Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    supervisorAdapter.notifyDataSetChanged();
                    dismissProgressDialog();
                    lv.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= 21){
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(getResources().getColor(R.color.yellow));
        }
        setContentView(R.layout.activity_supervisor_search);
        getSupportActionBar().hide();
        lv=findView(R.id.lv);
        search_text=findView(R.id.search_text);
        String keyword=getIntent().getStringExtra("keyword");
        search_text.setText(keyword);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(SupervisorSearchActivity.this,SupervisorDetailActivity.class);
                intent.putExtra("rowsList", (Serializable) rowsList);
                intent.putExtra("position",position);
                startActivity(intent);
            }
        });

        rowsList=new ArrayList<>();
        supervisorAdapter=new SupervisorAdapter(context,rowsList);
    }
    private void getSearchData(String str) {
        showProgressDialog();
        RequestParams params=new RequestParams(this);
        params.addFormDataPart("cmdtype","search");
        params.addFormDataPart("keyword",str);
        HttpRequest.get(HttpUrl.SUPERVISOR_QUERY,params,new JsonHttpRequestCallback(){
            @Override
            protected void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                parseJson(jsonObject);

            }

            @Override
            public void onFailure(int errorCode, String msg) {
                super.onFailure(errorCode, msg);
            }

            @Override
            public void onResponse(String response, Headers headers) {
                super.onResponse(response, headers);
            }
        });
    }
    private void parseJson(JSONObject jsonObject) {
        rowsList.clear();
        JSONArray jsonArray=jsonObject.getJSONArray("rows");
        for (int i=0;i<jsonArray.size();i++) {
            JSONObject jsonObject1=jsonArray.getJSONObject(i);
            Gson gson=new Gson();
            Rows rows=gson.fromJson(jsonObject1.toJSONString(),Rows.class);
            Log.d(TAG, "parseJson: "+rows.toString());
            rowsList.add(rows);
        }
        handler.sendEmptyMessage(0);
    }
    @Override
    public void initData() {
        lv.setAdapter(supervisorAdapter);
    }

    @Override
    public void addListeners() {
        search_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_SEARCH) {
                    if (TextUtils.isEmpty(search_text.getText().toString())) {
                        ToastUtils.showToast(context,"请输入关键字");
                    }else {

                        getSearchData(search_text.getText().toString());
                    }
                }
                return false;
            }
        });
    }
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;


        }
    }

    @Override
    public String getHttpTaskKey() {
        return HTTP_TASK_KEY;
    }
}
